#include<arpa/inet.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/socket.h>

using namespace std;


class Sock 
{
public:
  static int Socket()
  {
    int sock = socket(AF_INET,SOCK_STREAM,0); 
    if(sock <  0)
    {
      perror("socket");
      exit(1);
    }
    cout <<"Create Sock Success！" << endl;
    return sock;
  }
  //static void Bind(int sock,string ip,uint16_t port)
  static void Bind(int sock,uint16_t port)
  {
    struct sockaddr_in local;
    memset(&local,0,sizeof(local));
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    //local.sin_addr.s_addr = inet_addr(ip.c_str());
    local.sin_addr.s_addr = INADDR_ANY;
    socklen_t len = sizeof(local);
    if(bind(sock,(struct sockaddr*)&local,len) < 0)
    {
      perror("bind");
      exit(2);
    }
    cout << "Bind Success !" << endl;
  }
  static void Listen(int sock)
  {
    if(listen(sock,5) < 0)
    {
      perror("listen");
      exit(3);
    }
    cout <<"Listen Success !" << endl;
  }
  static int  Accept(int sock)
  {
    struct sockaddr_in peer;
    socklen_t len = sizeof(peer);
    bzero(&peer,len);
    int fd = accept(sock,(struct sockaddr*)&peer,&len);
    
    if(fd < 0)
    {
      perror("accept");
      exit(4);
    }
    cout << "Accept Success !" << endl;
    return fd;
  }
};
